<?php
include "../include/global.php";
include "../include/admin_check.php";
$xtpl = new XTemplate ("travelstyleWrite.htm");
include "setting.php";
include "../include/function.php";

$travelstyle_name=formatDataForMySql($_POST['travelstyle_name']);
$travelstyle_brief=formatDataForMySql($_POST['travelstyle_brief']);
$country_id=$_POST['country_id'];
$travelstyle_position=intval($_POST['travelstyle_position']);
$travelstyle_isactive=intval($_POST['travelstyle_isactive']);

$ID=intval(_POST('ID'));
$type=$_POST['type'];

//
$date = date("Y-m-d");
$sub_mit = $_POST['sub_mit'];
if($sub_mit=="yes"){
	if($travelstyle_name==""){
		$error_travelstyle_name ="Bạn phải nhập Tên <br />";
	}
	
	$str_error=$error_travelstyle_name;
	if($str_error==""){
		if($type=="ADD"){	
			$sql="INSERT INTO ".pre_table."travelstyle(travelstyle_name, travelstyle_brief, travelstyle_position, travelstyle_isactive, lang) 
					VALUES	('".$travelstyle_name."', '".$travelstyle_brief."', '".$travelstyle_position."', '".$travelstyle_isactive."', '".$lang."')";
			execSQL($sql);
			//
			$just_inserted_id=mysql_insert_id();			
			for($i=0;$i<count($country_id);$i++){
				$sql2="INSERT INTO ".pre_table."country_travelstyle (country_id, travelstyle_id) VALUES ('".$country_id[$i]."','".$just_inserted_id."')";
				$rs2=execSQL($sql2);
			}
		}else{
			$sql="UPDATE ".pre_table."travelstyle SET 
						travelstyle_name='".$travelstyle_name."', 
						travelstyle_brief='".$travelstyle_brief."', 
						travelstyle_position='".$travelstyle_position."', 
						travelstyle_isactive='".$travelstyle_isactive."',
						lang='".$lang."'
						WHERE travelstyle_id='".$ID."'";			
			execSQL($sql);
			//
			$sql="SELECT travelstyle_id FROM ".pre_table."travelstyle WHERE travelstyle_id='$ID'";
			$rs=execSQL($sql);
			if(mysql_num_rows($rs))
			{
				$old_relative=mysql_result($rs,0,0);
			}
			$sql="SELECT country_id FROM ".pre_table."country_travelstyle WHERE travelstyle_id='$ID'";
			$rs=execSQL($sql);
			$old_relative_arr=array();
			$i=0;
			while ($row=mysql_fetch_array($rs))
			{
				$old_relative_arr[$i]=$row["country_id"];
				$i++;
			}
			if(count($country_id)==0)
			{
				$sql_del="DELETE FROM ".pre_table."country_travelstyle WHERE travelstyle_id='$ID'";
				$rs_del=execSQL($sql_del);
			}
			else
			{
				for($j=0;$j<count($old_relative_arr);$j++)
				{
					if(!in_array($old_relative_arr[$j],$country_id))
					{
						$sql_del="DELETE FROM ".pre_table."country_travelstyle WHERE country_id='".$old_relative_arr[$j]."'  AND travelstyle_id = '$ID'";
						$rs_del=execSQL($sql_del);
					}
				}
				for($j=0;$j<count($country_id);$j++)
				{
					
					if(count($old_relative_arr)==0)
					{
						$sql_ins="INSERT INTO ".pre_table."country_travelstyle(country_id, travelstyle_id) VALUES ('".$country_id[$j]."','$old_relative')";
						$rs=execSQL($sql_ins);
					}
					else
					{
						if(!in_array($country_id[$j],$old_relative_arr))
						{
							$sql_ins="INSERT INTO ".pre_table."country_travelstyle(country_id, travelstyle_id) VALUES ('".$country_id[$j]."','$old_relative')";
							$rs=execSQL($sql_ins);
						}
					}
				}
			}
			//
		}			

		$act = $_POST['act'];
		if($act=='save'){
			echo '<script>
				parent.document.frmData.target = "_parent";				
				parent.window.location.href="travelstyle.php";
			</script>';
		}else{	
			echo '<script>
				parent.document.frmData.target = "_parent";	
				parent.document.frmData.travelstyle_name.focus();			
				parent.window.location.href="travelstyleWrite.php";
			</script>';
		}	
		
	}else{
		echo '<script>
				parent.document.frmData.target = "_parent";		
				parent.document.getElementById("err").style.display="block";
				parent.document.getElementById("err_string").innerHTML="'.$str_error.'";
			</script>';	
	}
}
?>